from PyQt5.QtCore import  QTranslator,QDir,pyqtSignal,pyqtSlot,pyqtProperty
from PyQt5.QtWidgets import qApp
import logging
import json
cEnglisthStr = "English";
cChineseStr = "简体中文";
class Trans(QTranslator):
    langLoaded=pyqtSignal()
    def __init__(self,parent=None):
        super().__init__(parent)
        self.m_map={}
    def beforeUiReady(self,ctx,folder):#QQmlContext* ctx, const QString& folder)
        self.m_ctx = ctx;
        ctx.setContextProperty("trans", self)
        if folder=="":
            self.loadFolder(qApp.applicationDirPath() + "/Trans");
        else:
            self.loadFolder(folder);
        qApp.installTranslator(self)
    def afterUiReady(self):
        pass

    def translate(self, context,sourceText,disambiguation,n):#const char* context, const char* sourceText, const char* disambiguation, int n) const
        return sourceText        
        return trans(sourceText)
    def loadFolder(self, folder):
        return 
        # qWarning() << "Trans loadFolder" << folder;
        logging.info(folder)
        dir=QDir(folder);
        infos = dir.entryInfoList({ "language_*.json" }, QDir.Files);
        print(infos)
        lang=""
        for info in infos:
            self.load(lang, info.absoluteFilePath());

        self.initEnglish();
        langs = self.m_map.keys();
        if (langs.contains(cChineseStr)):
            langs.removeAll(cChineseStr);
            langs.push_front(cChineseStr);
        self.setLanguages(langs);
        if (self.m_map.contains(cChineseStr)):
            self.setCurrentLang(cChineseStr);
        else:
            self.setCurrentLang(cEnglisthStr);
        self.folderLoaded.emit(folder);

    def load(self, lang,  filePath):
        logging.info([lang,filePath])
        lang=""
        file=open(filePath,encoding="utf-8")
        rootObj=json.load(file)#,object_pairs_hook=OrderedDict)
        lang = rootObj["lang"]
        trans = rootObj["trans"]
        for i in trans:
            transObj = i
            key = transObj["key"]
            value = transObj["value"]
            print(lang,key,value)
            self.m_map[lang][key] = value;
            # 简体中文 Chinese 简体中文
        self.langLoaded.emit(lang);
        return True;

    def currentLang(self):
        return m_currentLang;

    def languages(self):
        return m_languages;
    # @pyqtSlot()
    transStringChanged=pyqtSignal()
    @pyqtProperty(str, notify=transStringChanged)
    def transString(self):
        # logging.info("here")
        return ""
        return m_transString;

    def initEnglish(self):
        if not self.m_map.contains(cEnglisthStr):
            map=QHash()
            if (self.m_map.contains(cChineseStr)):
                map = self.m_map.value(cChineseStr);
            else:
                map = self.m_map.value(self.m_map.keys().first());
            for  key in map.keys():
                self.m_map[cEnglisthStr][key] = key;

    def trans(self,source):
        logging.info("here")
        return ""
        return self.m_map[m_currentLang][source]#, source);

    def setCurrentLang(self, currentLang):
        if (self.m_currentLang == currentLang):
            return;

        self.m_currentLang = currentLang;
        self.currentLangChanged.emit(self.m_currentLang);

        self.m_ctx.engine().retranslate();

    def setLanguages(self, languages):
        if (self.m_languages == languages):
            return;

        self.m_languages = languages;
        self.languagesChanged.emit(m_languages);
